package 所有的题类.F.树;

import 所有的题类.ZA.链表.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class 路径总和 {

    public static void main(String[] args) {
        路径总和 a=new 路径总和();
        int[]array={1,2,3,4,5,6,7};
        TreeNode treeNode = arrayToTreeNode(array, 0, array.length - 1);
        boolean b = a.hasPathSum(treeNode, 7);
        System.out.println(b);
    }

    public boolean hasPathSum(TreeNode root, int targetSum) {
        List<Integer> list= new ArrayList();
        int sum=0;
        getDetp(root,list,sum);
        return list.contains(targetSum);
    }

    public void getDetp(TreeNode root,List<Integer> list,int sum){
        if(root==null){
            list.add(sum);
            return;
        }
        sum=sum+root.val;
        getDetp(root.left,list,sum);
        getDetp(root.right,list,sum);
    }

    public static TreeNode arrayToTreeNode(int[]array,int left,int right){
        TreeNode root=null;
        if(right>=left){
            int mid=(left+right+1)/2;
            root = new TreeNode(array[mid]);
            TreeNode leftNode = arrayToTreeNode(array, left, mid-1);
            TreeNode rightNode = arrayToTreeNode(array, mid + 1, right);
            root.left=leftNode;
            root.right=rightNode;
        }else{
            root=null;
        }
        return root;
    }
}



